package com.moduleapp.login;

import android.os.Bundle;
import android.text.TextUtils;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.common.event.LoginEvent;
import com.common.route.RouterPath;
import com.fragment4.service.UserServiceImpl;
import com.moduleapp.databinding.ActivityLoginBinding;

import org.greenrobot.eventbus.EventBus;

@Route(path = RouterPath.LOGIN_ACTIVITY)
public class LoginActivity extends AppCompatActivity {

    private ActivityLoginBinding binding;
    private UserServiceImpl userService;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityLoginBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        initService();
        initViews();
    }

    private void initService() {
        userService = UserServiceImpl.getInstance();

        // 确保服务已初始化
        if (userService != null) {
            userService.init(getApplicationContext());
        }
        /**
         * 如果已经登录了就直接到主页面中，这里的判断可以迁移到项目中的引导页中
         */
        if (userService.isLogin()) {

        }
    }

    private void initViews() {
        // 设置登录按钮点击事件
        binding.btnLogin.setOnClickListener(v -> validateAndLogin());

        // 设置注册按钮点击事件
        binding.btnRegister.setOnClickListener(v -> navigateToRegister());
    }

    private void validateAndLogin() {
        String username = binding.etUsername.getText().toString().trim();
        String password = binding.etPassword.getText().toString().trim();

        // 验证输入
        if (TextUtils.isEmpty(username)) {
            binding.etUsername.setError("用户名不能为空");
            binding.etUsername.requestFocus();
            return;
        }

        if (TextUtils.isEmpty(password)) {
            binding.etPassword.setError("密码不能为空");
            binding.etPassword.requestFocus();
            return;
        }

        // 模拟登录逻辑
        login(username, password);
    }

    private void login(String username, String password) {
        // 调用用户服务进行登录
        UserServiceImpl.getInstance().login(username, password, new UserServiceImpl.LoginCallback() {
            @Override
            public void onSuccess(String username, String userId) {
                runOnUiThread(() -> {
                    Toast.makeText(LoginActivity.this,
                            "登录成功！欢迎 " + username, Toast.LENGTH_SHORT).show();

                    // 登录成功后跳转
                });
            }

            @Override
            public void onFailure(String errorMessage) {
                runOnUiThread(() -> {

                    Toast.makeText(LoginActivity.this,
                            "登录失败: " + errorMessage, Toast.LENGTH_SHORT).show();
                });
            }

            @Override
            public void onError(Exception e) {
                runOnUiThread(() -> {

                    Toast.makeText(LoginActivity.this,
                            "登录异常: " + e.getMessage(), Toast.LENGTH_SHORT).show();
                });
            }
        });
    }

    private void navigateToRegister() {
        // 使用 ARouter 跳转到注册页面
        ARouter.getInstance()
                .build(RouterPath.REGISTER_ACTIVITY)
                .navigation(this);
    }
}
